package com.minivision.fdiot.rest;

import com.minivision.fdiot.annotation.Log;
import com.minivision.fdiot.annotation.NoAuth;
import com.minivision.fdiot.common.AuthParam;
import com.minivision.fdiot.common.RestResult;
import com.minivision.fdiot.entity.AppInfo;
import com.minivision.fdiot.exception.FacePlatException;
import com.minivision.fdiot.rest.param.app.AppCreateParam;
import com.minivision.fdiot.rest.param.app.AppUpdateParam;
import com.minivision.fdiot.rest.result.app.AppCreateResult;
import com.minivision.fdiot.rest.result.app.AppUpdateResult;
import com.minivision.fdiot.service.AppService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping(value = "api/v1/app", method = RequestMethod.POST)
@Api(tags = "AppInfoApi", value = "AppInfo Apis")
public class AppInfoApi {

  @Autowired
  private AppService appService;
  
  @Value("${app.create.api.hidden}")
  private boolean appCreateHidden;

  /**
   * 创建应用
   * @param param
   * @return
   * @throws FacePlatException
   */
  @RequestMapping(value = "create")
  @ApiOperation(value="创建应用", notes="创建应用，不需要鉴权，程序中不需要Token头，Swagger上可填个随意字符串即可")
  @NoAuth
  @Log(module = "AppInfoApi", operation = "创建应用")
  public RestResult<AppCreateResult> create(@Valid @ModelAttribute AppCreateParam param) throws FacePlatException {
    if (appCreateHidden) {
      throw new IllegalStateException("app create api is not exposed!");
    }
    RestResult<AppCreateResult> result = new RestResult<>();
    result.setData(appService.create(param));
    return result;
  }

  @RequestMapping(value = "update")
  @ApiOperation(value="修改应用", notes="修改应用")
  @Log(module = "AppInfoApi", operation = "修改应用")
  public RestResult<AppUpdateResult> update(@Valid @ModelAttribute AppUpdateParam param) throws FacePlatException {
    RestResult<AppUpdateResult> result = new RestResult<>();
    result.setData(appService.update(param));
    return result;
  }

  @RequestMapping(value = "delete")
  @ApiOperation(value="删除应用", notes="删除应用")
  @Log(module = "AppInfoApi", operation = "删除应用")
  public RestResult<?> delete(@Valid @ModelAttribute AuthParam appDelParam) throws FacePlatException {
    RestResult<?> result = new RestResult<>();
    appService.delete(appDelParam);
    return result;
  }

  @RequestMapping(value = "detail")
  @ApiOperation(value="查看应用信息", notes="查看应用信息")
  public RestResult<AppInfo> detail(@Valid @ModelAttribute AuthParam param) throws FacePlatException {
    RestResult<AppInfo> result = new RestResult<>();
    AppInfo appInfo = appService.getDetail(param);
    result.setData(appInfo);
    return result;
  }

}
